﻿using System;
using ART.Common;

namespace ART.Admini.Users
{
    public partial class UserDetail : System.Web.UI.Page
    {
        protected int Id = 0;
        private ART.BLL.B_TBUsers bll = new ART.BLL.B_TBUsers();
        private ART.Model.M_TBUsers model = new ART.Model.M_TBUsers();

        protected void Page_Load(object sender, EventArgs e)
        {
            Id = Common.LYRequest.GetInt("id", 0);
            if (!IsPostBack)
            {
                InitData();
            }
        }
        private void InitData()
        {
            JBind.BindList(dropGroupId, new BLL.B_UserGroup().GetList("").Tables[0], "GroupName", "id");
            if (Id > 0)
            {//edit
                model = bll.GetModel(Id);
                if (model != null)
                {
                    chkEnabled.Checked = model.Enabled == 1 ? true : false;
                    //txtPowerList.Text = model.PowerList;
                    txtUserName.Text = model.Account;
                    //txtPassWord.Text = model.PassWord;
                    litPWD.Text = "不修改请留空！";
                    dropGroupId.SelectedValue = model.UserGroupId.ToString();
                    
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs p1)
        {
            if (Id > 0)
            {
                #region __________Edit__________
                model = bll.GetModel(Id);
                model.Enabled = chkEnabled.Checked?1:0;
                //model.PowerList = txtPowerList.Text;
                model.Account = txtUserName.Text;
                model.UserGroupId = Utils.StrToInt(dropGroupId.SelectedValue, 0);
                if (txtPassWord.Text!="")
                {
                    model.PWD = Utils.SHA256(Utils.MD5(txtPassWord.Text));
                }
                if (bll.Update(model))
                {
                    //B_operationLog.CreateOperationLog(B_operationLog.operationLogType.edit, "", "");
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                }
                #endregion
            }
            else
            {
                #region __________Add__________
                model.Enabled = chkEnabled.Checked ? 1 : 0;
                //model.PowerList = txtPowerList.Text;
                model.Account = txtUserName.Text;
                model.UserGroupId = Utils.StrToInt(dropGroupId.SelectedValue, 0);
                model.PWD = Utils.SHA256(Utils.MD5(txtPassWord.Text));
                model.UserFrom = txtUserFrom.Text;
                model.AID = BLL.B_TBAdmin.AId;
                model.CreateTime = DateTime.Now;
                model.LastLoginIP = "";
                model.LastLoginTime = DateTime.Now;
                model.LoginTimes = 1;
                if (bll.Add(model) > 0)
                {
                    //B_operationLog.CreateOperationLog(B_operationLog.operationLogType.add, "", "");
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                }
                #endregion
            }
        }
    }
}
